1 /*
2  * Copyright (c) 2013 - Andre Roth <neolynx@gmail.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation version 2.1 of the License.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16  * Or, point your browser to http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
17  *
18  */
19 
20 /**
21  * @file mgt.h
22  * @ingroup dvb_table
23  * @brief Provides the table parser for the ATSC MGT (Master Guide Table)
24  * @copyright GNU Lesser General Public License version 2.1 (LGPLv2.1)
25  * @author Andre Roth
26  *
27  * @par Relevant specs
28  * The table described herein is defined at:
29  * - ATSC A/65:2009
30  *
31  * @see
32  * http://www.etherguidesystems.com/help/sdos/atsc/syntax/tablesections/MGT.aspx
33  *
34  * @par Bug Report
35  * Please submit bug reports and patches to linux-media@vger.kernel.org
36  */
37 
38 module libdvbv5_d.mgt;
39 
40 import core.sys.posix.unistd;
41 
42 import libdvbv5_d.descriptors: dvb_desc;
43 import libdvbv5_d.dvb_fe: dvb_v5_fe_parms;
44 import libdvbv5_d.header: dvb_table_header;
45 
46 extern (C):
47 
48 /* ssize_t */
49 
50 /**
51  * @def ATSC_TABLE_MGT
52  *	@brief ATSC MGT table ID
53  *	@ingroup dvb_table
54  */
55 enum ATSC_TABLE_MGT = 0xC7;
56 
57 /**
58  * @struct atsc_table_mgt_table
59  * @brief ATSC tables descrition at MGT table
60  * @ingroup dvb_table
61  *
62  * @param type		table type
63  * @param pid		table type pid
64  * @param type_version	type type version number
65  * @param size		number of bytes for the table entry
66  * @param desc_length	table type descriptors length
67  * @param descriptor	pointer to struct dvb_desc
68  * @param next		pointer to struct atsc_table_mgt_table
69  *
70  * This structure is used to store the original VCT channel table,
71  * converting the integer fields to the CPU endianness.
72  *
73  * The undocumented parameters are used only internally by the API and/or
74  * are fields that are reserved. They shouldn't be used, as they may change
75  * on future API releases.
76  *
77  * Everything after atsc_table_mgt_table::descriptor (including it) won't
78  * be bit-mapped * to the data parsed from the MPEG TS. So, metadata are
79  * added there.
80  */
81 struct atsc_table_mgt_table
82 {
83     import std.bitmanip : bitfields;
84     align (1):
85 
86     ushort type;
87 
88     union
89     {
90         align (1):
91 
92         ushort bitfield;
93 
94         struct
95         {
96             import std.bitmanip : bitfields;
97             align (1):
98 
99             mixin(bitfields!(
100                 ushort, "pid", 13,
101                 ushort, "one", 3));
102         }
103     }
104 
105     mixin(bitfields!(
106         ubyte, "type_version", 5,
107         ubyte, "one2", 3));
108 
109     uint size;
110 
111     union
112     {
113         align (1):
114 
115         ushort bitfield2;
116 
117         struct
118         {
119             import std.bitmanip : bitfields;
120             align (1):
121 
122             mixin(bitfields!(
123                 ushort, "desc_length", 12,
124                 ushort, "one3", 4));
125         }
126     }
127 
128     // struct dvb_desc;
129     dvb_desc* descriptor;
130     atsc_table_mgt_table* next;
131 }
132 
133 /**
134  * @struct atsc_table_mgt
135  * @brief ATSC MGT table
136  * @ingroup dvb_table
137  *
138  * @param header		struct dvb_table_header content
139  * @param protocol_version	protocol version
140  * @param tables		tables_defined Number of tables defined
141  * @param table			pointer to struct atsc_table_mgt_table
142  * @param descriptor		pointer to struct dvb_desc
143  *
144  * This structure is used to store the original MGT channel table,
145  * converting the integer fields to the CPU endianness.
146  *
147  * The undocumented parameters are used only internally by the API and/or
148  * are fields that are reserved. They shouldn't be used, as they may change
149  * on future API releases.
150  *
151  * Everything after atsc_table_mgt::table (including it) won't
152  * be bit-mapped * to the data parsed from the MPEG TS. So, metadata are
153  * added there.
154  */
155 struct atsc_table_mgt
156 {
157     align (1):
158 
159     dvb_table_header header;
160     ubyte protocol_version;
161     ushort tables;
162     atsc_table_mgt_table* table;
163     dvb_desc* descriptor;
164 }
165 
166 /**
167  * @brief Macro used to find a table inside a MGT table
168  *
169  * @param _table	channel to seek
170  * @param _mgt		pointer to struct atsc_table_mgt_table
171  */
172 
173 // struct dvb_v5_fe_parms;
174 
175 /**
176  * @brief Initializes and parses MGT table
177  * @ingroup dvb_table
178  *
179  * @param parms	struct dvb_v5_fe_parms pointer to the opened device
180  * @param buf buffer containing the MGT raw data
181  * @param buflen length of the buffer
182  * @param table pointer to struct atsc_table_mgt to be allocated and filled
183  *
184  * This function allocates an ATSC MGT table and fills the fields inside
185  * the struct. It also makes sure that all fields will follow the CPU
186  * endianness. Due to that, the content of the buffer may change.
187  *
188  * @return On success, it returns the size of the allocated struct.
189  *	   A negative value indicates an error.
190  */
191 ssize_t atsc_table_mgt_init (
192     dvb_v5_fe_parms* parms,
193     const(ubyte)* buf,
194     ssize_t buflen,
195     atsc_table_mgt** table);
196 
197 /**
198  * @brief Frees all data allocated by the MGT table parser
199  * @ingroup dvb_table
200  *
201  * @param table pointer to struct atsc_table_mgt to be freed
202  */
203 void atsc_table_mgt_free (atsc_table_mgt* table);
204 
205 /**
206  * @brief Prints the content of the MGT table
207  * @ingroup dvb_table
208  *
209  * @param parms	struct dvb_v5_fe_parms pointer to the opened device
210  * @param table pointer to struct atsc_table_mgt
211  */
212 void atsc_table_mgt_print (dvb_v5_fe_parms* parms, atsc_table_mgt* table);